/*
* UserManager.java
* Created on  2013-10-21 上午7:44
* 版本       修改时间          作者      修改内容
* V1.0.1    2013-10-21       gaoxinyu    初始版本
*
*/
package com.genghis.ptas.security.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.security.dao.UserDao;
import com.genghis.ptas.security.dao.UserRoleDao;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.service.UserService;
import com.genghis.ptas.system.entity.DictValues;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.util.LoginUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author gaoxinyu
 * @version 1.0.1
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private DictService dictService;

    @Autowired
    private UserRoleDao userRoleDao;

    @Override
    public Page<User> findUserPageList(PageParameter pageParameter, User user) {
        Page<User> users = userDao.findUserPageList(pageParameter, user);
        for (User oneUser : users.getResultList()) {
            oneUser.setShowSex(dictService.changeDictCodeToValue(oneUser.getSex(), DictConstant.SEX));
        }
        return users;
    }


    @Override
        public Page<User> findAdministratorPageList(PageParameter pageParameter, User user) {
            Page<User> users = userDao.findAdministratorPageList(pageParameter, user);
            for(User user1:users.getResultList()){
               user1.setShowSex(dictService.changeDictCodeToValue(user1.getSex(), DictConstant.SEX));
               user1.setShowUserRole(dictService.changeDictCodeToValue(user1.getUserRole(),DictConstant.USER_ROLE));
            }
        return users;
    }

    @Override
    public Page<User> findConsumerPageList(PageParameter pageParameter, User user) {
        Page<User> users = userDao.findConsumerPageList(pageParameter, user);
        for(User user1:users.getResultList()){
            user1.setShowSex(dictService.changeDictCodeToValue(user1.getSex(), DictConstant.SEX));
            user1.setShowUserRole(dictService.changeDictCodeToValue(user1.getUserRole(),DictConstant.USER_ROLE));
        }
        return users;
    }

    @Override
    public User findUserAllInfoById(int id) {
        User user = userDao.findUserAllInfoById(id);
//        user.setShowDepartmentId(dictService.changeDictCodeToValue(user.getDepartmentId(), DictConstant.DEPARTMENT));
        return user;
    }

    @Override
    public User findUserByLoginName(String loginName) {
        return userDao.findUserByLoginName(loginName);
    }

    @Override
    public Boolean checkRepeatLoginName(String loginName, String oldName) {
        Boolean hasLoginName = userDao.checkRepeatLoginName(loginName);
        return !(hasLoginName && !oldName.equals(loginName));
    }

    @Override
    public Boolean checkRepeatUserName(String userName, String oldName) {
        Boolean hasLoginName = userDao.checkRepeatUserName(userName);
        return !(hasLoginName && !oldName.equals(userName));
    }

    @Override
    public void addUser(User user) {
        user.setUserRole(0);
        userDao.addUser(user);
    }

    @Override
        public void addAdministrator(User user) {
            user.setUserRole(1);
            int id = userDao.addAdministrator(user);
        }

    @Override
    public void editUser(User user) {
        userDao.editUser(user);
    }

    public void editAdministrator(User user) {
            userDao.editAdministrator(user);
        }

    @Override
    public void delUsers(String userIds[]) {
        for (String userId : userIds) {
            userDao.delUser(Integer.parseInt(userId));
            userRoleDao.deleteUserRoleByUserId(userId);
        }
    }

    @Override
    public void delAdministrator(String userIds[]) {
        for (String userId : userIds) {
            userDao.delAdministrator(Integer.parseInt(userId));
        }
    }

    @Override
    public List<DictValues> findAllCourseType() {
        List<DictValues>  courseType=userDao.findAllCourseType();
        return courseType;
    }

    public Boolean checkPassword(String password) {
        User user = findUserByLoginName(LoginUserUtil.findLoginUserName());
        return password.equals(user.getPassword());
    }

    @Override
    public void changeUserPassword(User user, String newPassword) {
        user.setPassword(newPassword);
        userDao.changePassword(user);
    }
}